#!/bin/bash
# 清除AI录取分数线的Redis缓存
# 用于解决缓存Key不包含province和batch导致的数据错误问题

echo "========================================="
echo "清除 AI 录取分数线缓存"
echo "========================================="
echo ""

# Redis连接信息（请根据实际情况修改）
REDIS_HOST="${REDIS_HOST:-localhost}"
REDIS_PORT="${REDIS_PORT:-6379}"
REDIS_PASSWORD="${REDIS_PASSWORD:-}"
REDIS_DB="${REDIS_DB:-0}"

# 构建Redis命令
if [ -n "$REDIS_PASSWORD" ]; then
    REDIS_CLI="redis-cli -h $REDIS_HOST -p $REDIS_PORT -a $REDIS_PASSWORD -n $REDIS_DB"
else
    REDIS_CLI="redis-cli -h $REDIS_HOST -p $REDIS_PORT -n $REDIS_DB"
fi

echo "Redis连接信息："
echo "  Host: $REDIS_HOST"
echo "  Port: $REDIS_PORT"
echo "  Database: $REDIS_DB"
echo ""

# 查看当前缓存数量
echo "1. 查看当前AI录取分数线缓存数量..."
CURRENT_COUNT=$($REDIS_CLI KEYS "ai:cutoff:scores::*" | wc -l)
echo "   当前缓存数量: $CURRENT_COUNT"
echo ""

if [ $CURRENT_COUNT -eq 0 ]; then
    echo "   没有找到需要清除的缓存"
    exit 0
fi

# 显示部分缓存key示例
echo "2. 缓存Key示例（前5个）："
$REDIS_CLI KEYS "ai:cutoff:scores::*" | head -5
echo ""

# 询问是否继续
read -p "是否要清除这些缓存？(y/n) " -n 1 -r
echo ""

if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "3. 正在清除缓存..."
    $REDIS_CLI --scan --pattern "ai:cutoff:scores::*" | xargs -L 1000 $REDIS_CLI DEL
    echo "   ✓ 缓存已清除"
    echo ""
    
    # 验证
    NEW_COUNT=$($REDIS_CLI KEYS "ai:cutoff:scores::*" | wc -l)
    echo "4. 验证结果："
    echo "   清除前数量: $CURRENT_COUNT"
    echo "   清除后数量: $NEW_COUNT"
    echo ""
    
    if [ $NEW_COUNT -eq 0 ]; then
        echo "✓ 缓存清除成功！"
    else
        echo "⚠ 警告：仍有 $NEW_COUNT 个缓存未清除"
    fi
else
    echo "已取消清除操作"
fi

echo ""
echo "========================================="
echo "完成"
echo "========================================="

